package cn.com.lms.business.exam.service;

import cn.com.lms.business.core.constant.ErrorEnum;
import cn.com.lms.business.core.constant.BNZConstants;
import cn.com.lms.business.core.util.SystemUserUtil;
import cn.com.lms.business.exam.enums.QuickScoreTypeEnum;
import cn.com.lms.business.exam.persistence.entity.PaperQuickScore;
import cn.com.lms.business.exam.persistence.repository.PaperQuickScoreRepository;
import cn.com.lms.business.user.persistence.entity.SysUserVo;


import com.bnzj.core.exception.BusinessException;
import com.bnzj.core.persistence.BaseService;

import com.bnzj.core.util.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

@Service
public class PaperQuickScoreService extends BaseService<PaperQuickScoreRepository, PaperQuickScore,Long> {

    public PaperQuickScore updatePaperQuickScore(PaperQuickScore paperQuickScore){

        Optional<PaperQuickScore> paperQuickScoreOpt = findById(paperQuickScore.getId());
        if (!paperQuickScoreOpt.isPresent()) {
            throw new BusinessException(ErrorEnum.RECORD_NOT_EXIST.getMessage(), "" + ErrorEnum.RECORD_NOT_EXIST.getCode());
        }
        PaperQuickScore target=paperQuickScoreOpt.get();
        BeanUtils.copyProperties(paperQuickScore,target);
        save(target);
        return  target;
    }

    /**
     * 获取快捷分数设置，没有系统自动生成一条
     * @return
     */
    public PaperQuickScore  getQuickScore(){
        PaperQuickScore queryEntity=new PaperQuickScore();
        queryEntity.setCodeNumber(BNZConstants.CODE_NUMBER);
        Optional<PaperQuickScore> paperQuickScoreOpt = findOne(queryEntity);
        if (!paperQuickScoreOpt.isPresent()) {
            SysUserVo sysUser = SystemUserUtil.getCurrentUser();
            PaperQuickScore paperQuickScore=new PaperQuickScore();
            paperQuickScore.setSingleScore(2);
            paperQuickScore.setMultipleScore(2);
            paperQuickScore.setJudgeScore(2);
            paperQuickScore.setFillScore(2);
            paperQuickScore.setExplainScore(6);
            paperQuickScore.setCreator(String.valueOf(sysUser.getId()));
            paperQuickScore.setCreateTime(new Date());
            paperQuickScore.setCreatorName(sysUser.getCname());
            paperQuickScore.setType(QuickScoreTypeEnum.SET_BY_SUBJECT.getValue());
            paperQuickScore.setCodeNumber(BNZConstants.CODE_NUMBER);
            PaperQuickScore result=   save(paperQuickScore);
            return result;
        }
        return paperQuickScoreOpt.get();
    }
}
